package com.situ.student.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import com.situ.student.controller.StudentController;
import com.situ.student.entity.Student;

public class UpdateStudentFrame extends UpdateShareFrame {
	private StudentController studentController = new StudentController();
	private MainFrame mainFrame;
	private Student selectStudent;

	public UpdateStudentFrame(Student selectStudent, MainFrame mainFrame) {
		this.selectStudent = selectStudent;
		this.mainFrame = mainFrame;
	}

	public void init() {
		super.init();
		idText.setText(String.valueOf(selectStudent.getId()));
		nameText.setText(selectStudent.getName());
		ageText.setText(String.valueOf(selectStudent.getAge()));
		genderText.setText(selectStudent.getGender());
		saveButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("UpdateStudentFrame.init()");
				int id = Integer.parseInt(idText.getText());
				String name = nameText.getText();
				String gender = genderText.getText();
				int age = Integer.parseInt(ageText.getText());

				selectStudent.setId(id);
				selectStudent.setName(name);
				selectStudent.setGender(gender);
				selectStudent.setAge(age);

				System.out.println(id);
				System.out.println(name);
				System.out.println(age);
				System.out.println(gender);

				boolean result = studentController.updateStudent(id, name, age, gender);
				if (result) {// 添加成功，弹出保存成功窗口，刷新表格
					// 保存成功或者失败，弹出提示框
					JOptionPane.showMessageDialog(null, "修改成功");
					mainFrame.reloadTable();
				} else {
					JOptionPane.showMessageDialog(null, "修改失败");
				}
				// 关闭窗口
				UpdateStudentFrame.this.dispose();
			}
		});
	}
}
